using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class SearchResults : System.Web.UI.Page
{
    // Total Number of books in array
    private int _numberOfBooks;
    public int NumberOfBooks
    {
        get { return _numberOfBooks; }
        set { _numberOfBooks = value; }
    }

    //Current page that is being displayed
    private int _pageNumber;
    public int CurrentPageNumber
    {
        get { return _pageNumber; }
        set { _pageNumber = value; }
    }

    //Number of pages required to display info
    private int _totalNumberOfPages;
    public int TotalNumberOfPages
    {
        get { return _totalNumberOfPages; }
        set { _totalNumberOfPages = value; }
    }

    private int _booksPerPage;

    public int BooksPerPage
    {
        get { return _booksPerPage; }
        set { _booksPerPage = value; }
    }
	
	
    protected void Page_Init(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            String Isbn, Title, Author, SearchType, pageNum;
            Isbn = Request.QueryString["txtISBN"];
            Title = Request.QueryString["txtTitle"] ;
            Author = Request.QueryString["txtAuthor"] ;
            SearchType = Request.QueryString["txtSearchType"];
            pageNum = Request.QueryString["txtPageNumber"];

            //Get the Current Page Number
            if (pageNum != null)
            {
                CurrentPageNumber = Convert.ToInt32(pageNum);
            }
            else
            {
                CurrentPageNumber = 1;
            }

            
            //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*********************************

            Response.Write(Isbn);
            //Create test objects\\

            IntegrationGroup.Book testBook1 = IntegrationGroup.TestObjects.GetBook1();
            IntegrationGroup.Book testBook2 = IntegrationGroup.TestObjects.GetBook2();
            IntegrationGroup.Book testBook3 = IntegrationGroup.TestObjects.GetBook2();
            IntegrationGroup.Book testBook4 = IntegrationGroup.TestObjects.GetBook2();
            IntegrationGroup.Book testBook5 = IntegrationGroup.TestObjects.GetBook2();
            IntegrationGroup.Book testBook6 = IntegrationGroup.TestObjects.GetBook2();
            IntegrationGroup.Book testBook7 = IntegrationGroup.TestObjects.GetBook2();
            IntegrationGroup.Book testBook8 = IntegrationGroup.TestObjects.GetBook2();
            IntegrationGroup.Book testBook9 = IntegrationGroup.TestObjects.GetBook2();
            IntegrationGroup.Book testBook10 = IntegrationGroup.TestObjects.GetBook2();
            IntegrationGroup.Book testBook11 = IntegrationGroup.TestObjects.GetBook2();
            IntegrationGroup.Book testBook12 = IntegrationGroup.TestObjects.GetBook2();
            IntegrationGroup.Book testBook13 = IntegrationGroup.TestObjects.GetBook2();

            IntegrationGroup.Book[] bookArray = { testBook1, testBook2, testBook3, testBook4, testBook5, testBook6, testBook7, testBook8, testBook9, testBook10, testBook11, testBook12, testBook13 };

            //ADD THIS TO MAKE NO BOOKS FOUND!!!\\
            //IntegrationGroup.Book[] bookArray ={ };

            Session.Add("bookArray", bookArray);
            
            BooksPerPage= 10;
            int totalBooks = bookArray.Length;
            TotalNumberOfPages = 5;

            TotalNumberOfPages = totalBooks / BooksPerPage;
            if ((totalBooks % BooksPerPage) != 0)
            {
                TotalNumberOfPages++;
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //If there are no books found, create the array with size 0 to specifiy there are no books.
        IntegrationGroup.Book[] bookArray ={ };
        bookArray = (IntegrationGroup.Book[])Session["bookArray"];
        if (bookArray == null)
        {
            bookArray = new IntegrationGroup.Book[0];
        }

        NumberOfBooks = bookArray.Length;
        
        //If there are no books in the array, do not display this table.
        if (NumberOfBooks == 0)
        {
            TableRow noBooksFound = new TableRow();
            noBooksFound.BackColor = System.Drawing.Color.White;

            TableCell noBooksMessage = new TableCell();
            noBooksMessage.Controls.Add(new LiteralControl("No books were found, please search again"));
            noBooksFound.Cells.Add(noBooksMessage);

            TableCell backButton = new TableCell();
            backButton.Controls.Add(new LiteralControl("<a href=\"http://www.psu.edu\"> Back </a>"));
            noBooksFound.Cells.Add(backButton);

            Table1.Rows.Add(noBooksFound);
        }
        else
        {
            //Add the Table Headers
            AddTableHeaders();

            
            int tempBooksPerPage = BooksPerPage;

            //Determind if it is the last page
            if (CurrentPageNumber == TotalNumberOfPages)
            {
                int tempBookRemainder = (TotalNumberOfPages-1) * BooksPerPage;

                tempBooksPerPage = NumberOfBooks - tempBookRemainder;
            }

            //////////////// ADD the BOOKS to the table \\\\\\\\\\\\\\\\\\\\\\\\
            for (int j = 0; j < tempBooksPerPage; j++)
            {
                TableRow r = new TableRow();
                if ((j % 2) == 0)
                {
                    r.BackColor = System.Drawing.Color.LightGray;
                }
                else
                {
                    r.BackColor = System.Drawing.Color.White;
                }

                TableCell picCell = new TableCell();
                picCell.Controls.Add(new LiteralControl("<a href=\"http://www.psu.edu\"><img src=\"" + bookArray[j].ImageUrl + "\"  width=\"50\" alt=" + bookArray[j].Title + " height=\"75\" border=\"0\" /></a>"));
                //picCell.Controls.Add(new LiteralControl("<a href=Response.Redirect(\"ist411svr.ist.psu.edu/BookProfile.aspx?ID=" + bookArray[j].ID)+"><img src=\"" + bookArray[j].ImageUrl + "\"  width=\"50\" alt=" + bookArray[j].Title + " height=\"75\" border=\"0\" /></a>");
                r.Cells.Add(picCell);

                
                //

                TableCell titleCell = new TableCell();
                titleCell.Controls.Add(new LiteralControl(bookArray[j].Title));
                r.Cells.Add(titleCell);

                TableCell authorCell = new TableCell();
                authorCell.Controls.Add(new LiteralControl(bookArray[j].Author));
                r.Cells.Add(authorCell);

                TableCell isbnCell = new TableCell();
                isbnCell.Controls.Add(new LiteralControl(bookArray[j].ISBN));
                r.Cells.Add(isbnCell);

                TableCell viewPublisher = new TableCell();
                viewPublisher.Controls.Add(new LiteralControl(bookArray[j].Publisher));
                r.Cells.Add(viewPublisher);

                TableCell viewPageNum = new TableCell();
                viewPageNum.Controls.Add(new LiteralControl((bookArray[j].NumPages).ToString()));
                r.Cells.Add(viewPageNum);

                TableCell viewLanguage = new TableCell();
                viewLanguage.Controls.Add(new LiteralControl(bookArray[j].Language));
                r.Cells.Add(viewLanguage);

                /*TableCell viewTradeCell = new TableCell();
                viewTradeCell.Controls.Add(new LiteralControl("<a href=\"http://www.psu.edu\">I Want This Book </a>"));
                r.Cells.Add(viewTradeCell);
                */

                Table1.Rows.Add(r);        
            }

        //Add the Page Numbers Table
        AddPageNumbersTable();

        }

    }

    public void AddPageNumbersTable()
    {
        int pageDisplayNumber = 0;
        TableRow PageNumbersRow = new TableRow();
        Boolean previousButton = false;
        Boolean nextButton = false;

        TableCell[] SpecificPageNumCell = new TableCell[5];

        //Create the previous link
        TableCell PreviousPageCell = new TableCell();
        PreviousPageCell.Controls.Add(new LiteralControl("<a href=\"SearchResults.aspx?txtPageNumber=" + (CurrentPageNumber-1)+ "\"><< Previous</a>"));
        PageNumbersRow.Cells.Add(PreviousPageCell);

        for (int i = 0; i < 5; i++)
        {
            Boolean IsPage = false;
            SpecificPageNumCell[i] = new TableCell();

            // Position to the left 2.
            // Also if it is the 1st or 2nd page, do not add.
            if ((i == 0) && ((CurrentPageNumber != 1) && (CurrentPageNumber != 2)))
            {
                pageDisplayNumber = i - 2 + CurrentPageNumber;
                IsPage = true;
                previousButton = true;
            }

            // Position to the left 1.
            // Also if it is the 1stpage, do not add.
            else if ((i == 1) && (CurrentPageNumber != 1))
            {
                pageDisplayNumber = i - 2 + CurrentPageNumber;
                IsPage = true;
                previousButton = true;
            }

            // Position of the page displayed.
            // Also if it is the 1st or 2nd page, do not add.
            else if (i == 2)
            {
                pageDisplayNumber = CurrentPageNumber;
                IsPage = true;
            }

            // Position to the right 1.
            // Also if it is the last page, do not add.
            else if ((i == 3) && (TotalNumberOfPages >= (CurrentPageNumber + 1)))
            {
                pageDisplayNumber = i - 2 + CurrentPageNumber;
                IsPage = true;
                nextButton = true;
            }

            // Position to the right 2.
            // Also if it is the 2nd to last or last page, do not add.
            else if ((i == 4) && (TotalNumberOfPages >= (CurrentPageNumber + 2)))
            {
                pageDisplayNumber = i - 2 + CurrentPageNumber;
                IsPage = true;
                nextButton = true;
            }

            //If the page is valid - add the corresponding link
            if (IsPage == true)
            {
                // Do not set the active page to a link
                String linkString = "";
                if (i != 2)
                {
                    //linkString = "<a href=\"http://www.psu.edu" + pageDisplayNumber + "\">" + pageDisplayNumber + "</a>";
                    linkString = "<a href=\"SearchResults.aspx?txtPageNumber=" + pageDisplayNumber+ "\">" + pageDisplayNumber + "</a>";
                    //Response.Redirect("~/SearchResults.aspx?txtPageNumber=" + pageDisplayNumber);
                }
                else
                {
                    linkString = Convert.ToString(pageDisplayNumber);
                }
                SpecificPageNumCell[i].Controls.Add(new LiteralControl(linkString));
                PageNumbersRow.Cells.Add(SpecificPageNumCell[i]);
            }
        }
        //Add Previous and Next links
        if (!previousButton)
        {
            PageNumbersRow.Controls.Remove(PreviousPageCell);
        }

        if (nextButton)
        {
            //Create the previous link
            TableCell NextPageCell = new TableCell();
            NextPageCell.Controls.Add(new LiteralControl("<a href=\"SearchResults.aspx?txtPageNumber=" + (CurrentPageNumber+1)+ "\">Next >></a>"));
            PageNumbersRow.Cells.Add(NextPageCell);
        }
        
        PagesTable.Rows.Add(PageNumbersRow);
    }

    public void AddTableHeaders()
    {

        //Add the table Headers\\
        int numHeaders = 8;
        TableHeaderCell[] tableHeaders = new TableHeaderCell[numHeaders];
        String[] tableHeadersText = { "Picture", "Title", "Author", "ISBN", "Publisher", "Pages", "Language", " " };

        TableRow headerRow = new TableRow();
        headerRow.BackColor = System.Drawing.Color.White;

        for (int i = 0; i < numHeaders; i++)
        {
            tableHeaders[i] = new TableHeaderCell();
            tableHeaders[i].Controls.Add(new LiteralControl(tableHeadersText[i]));
            headerRow.Cells.Add(tableHeaders[i]);
        }
        Table1.Rows.Add(headerRow);

    }
}
